package com.itheima.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.dao.cargo.ContractDao;
import com.itheima.dao.cargo.ContractProductDao;
import com.itheima.dao.cargo.ExtCproductDao;
import com.itheima.doamin.cargo.*;
import com.itheima.service.cargo.ContractProductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class ContractProductServiceImpl implements ContractProductService{
    @Autowired
    ContractProductDao contractProductDao;
    //合同dao接口
    @Autowired
    ContractDao contractDao;
    //附件dao接口
    @Autowired
    ExtCproductDao extCproductDao;

    /**
     * 保存
     *
     * @param contractProduct
     */
    @Override
    public void save(ContractProduct contractProduct) {
        //进行保存操作
        //货物的总价
        double amount=0;
        //判断用户是否输入了数量和单价
        if (contractProduct.getCnumber()!=null && contractProduct.getPrice()!=null){
            amount=contractProduct.getCnumber()*contractProduct.getPrice();
        }
        //id
        contractProduct.setId(UtilFuns.getUUID());
        //设置货物的总价
        contractProduct.setAmount(amount);
        //获得合同的dao对象,设置合同的总金
        //创建合同的条件对象
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        contract.setUpdateTime(new Date());
        //更新货物的单价contract.getProNum()+1\
        int cProNum=contract.getProNum()+1;
        contract.setProNum(cProNum);
        contract.setTotalAmount(contract.getTotalAmount()+amount);
        //向数据库进行插入
        contractProductDao.insertSelective(contractProduct);
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /**
     * 更新
     *
     * @param contractProduct
     */
    @Override
    public void update(ContractProduct contractProduct) {
        //更新货物
        //判断用户是否输入了价格和总金额,用户新输入的货物总价
        double newAmount=0d;
        if (contractProduct.getPrice()!=null && contractProduct.getCnumber()!=null){
            newAmount=contractProduct.getPrice()*contractProduct.getCnumber();
        }
        //用户没输入值之前货物的总价
        //数据库查旧值
        double oldAmount=contractProductDao.selectByPrimaryKey(contractProduct.getId()).getAmount();
        //更新货物的总价
        contractProduct.setAmount(newAmount);
        //更新合同
        //通过合同的id查询合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //设置合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()-oldAmount+newAmount);
        //对货物表和合同表进行更新
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        //根据id取出货物
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        //创建附件的查询条件
        ExtCproductExample extCproductExample=new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractProductIdEqualTo(id);
        //使用条件进行查询
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
        //获得附件的总价格
        double extAmount=0d;
        int num=0;
        //对
        if (UtilFuns.isNotEmpty(extCproducts)){
            //集合不为空
            for (int i=0;i<extCproducts.size();i++) {
                num++;
                extAmount+=extCproducts.get(i).getAmount();
                //对数据库中的附件,进行删除
                extCproductDao.deleteByPrimaryKey(extCproducts.get(i).getId());
            }
        }

        //对货物进行删除
        contractProductDao.deleteByPrimaryKey(id);

        //对合同下的数据进行操作
        //根据货物中对应的合同id获得对应的合同对象
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount()-contractProduct.getAmount()-extAmount);
        contract.setProNum(contract.getProNum()-1);
        contract.setExtNum(contract.getExtNum()-num);
        //对合同进行更新
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /**
     * 根据id查询
     *
     * @param id
     */
    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public void findAllByList(List<ContractProduct> contractProducts) {
        for (ContractProduct contractProduct:contractProducts){
            save(contractProduct);
        }
    }

    @Override
    public List getSellData(String companyId) {
        return contractProductDao.selectAllSell(companyId);
    }

    @Override
    public List getFactorySellData(String companyId) {
        return contractProductDao.selectFactorySellData(companyId);
    }



    /**
     * 分页查询
     *
     * @param example
     * @param page
     * @param size
     */
    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ContractProduct> contractProducts = contractProductDao.selectByExample(example);
        return new PageInfo(contractProducts);
    }



}
